#import "PopulationGame.h"

@interface PopulationGame ()


@property BOOL isGameOver;
@property NSArray* colors;
@property NSMutableDictionary* answers;
@property NSArray* keys;
@property NSInteger maxNumberOfWrongAnswers;
@property NSString* currentCountryOne;
@property NSString* currentCountryTwo;

@end


@implementation PopulationGame


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"PopulationGame"];
        [self setType:knowleadge];
        self.ruleOfTheGame=@"Who has a bigger population?";
        self.answers=[[NSMutableDictionary alloc]init];
        [self.answers setObject:@7166169 forKey:@"Bulgaria"];
        [self.answers setObject:@66030000 forKey:@"France"];
        [self.answers setObject:@316000000 forKey:@"USA"];
        [self.answers setObject:@200400000 forKey:@"Brazil"];
        [self.answers setObject:@64000000 forKey:@"UK"];
        [self.answers setObject:@80620000 forKey:@"Germany"];
        [self.answers setObject:@47270000 forKey:@"Spain"];
        [self.answers setObject:@11200000 forKey:@"Belgium"];
        [self.answers setObject:@9593000 forKey:@"Sweden"];
        [self.answers setObject:@10460000 forKey:@"Portugal"];
        
        
        self.keys=[self.answers allKeys];
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
        self.maxNumberOfWrongAnswers=arc4random_uniform(kMaxNumberOfWrongAnswer);
    }
    return self;
}

-(void)draw{
    [self removeAllComponents];
    
    int firstNumber=arc4random_uniform(kPopulationGameNumberOfCountry);
    int secondNumber=arc4random_uniform(kPopulationGameNumberOfCountry);
    
    
    int colour=arc4random_uniform(kColorNumber);
    
    
    self.currentCountryOne=self.keys[firstNumber];
    self.currentCountryTwo=self.keys[secondNumber];
    
     
    
    NSString* labelText=[NSString stringWithFormat:@"%@ > %@",self.currentCountryOne,self.currentCountryTwo];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:colour]];
    
    
    
    
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:(kScreenWidth/kWidthDel)/2 andColour:labelColor];
    
    [self addComponents:label];
    [self addComponents:labelOne];
    
}

-(BOOL)isRight{
    NSInteger a=[[self.answers objectForKey:self.currentCountryOne] integerValue];
    NSInteger b=[[self.answers objectForKey:self.currentCountryTwo] integerValue];
    if (a > b) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
    NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval: 2                                                 target: self selector:@selector(newContries:)userInfo: nil repeats:YES];
    
}

-(void)newContries:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"SomeGames"
         object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}

@end
